/*
 * @Author: chengsl
 * @Date: 2023-04-02 11:21:04
 * @LastEditors: chengsl
 * @LastEditTime: 2023-04-26 16:44:42
 * @Description: 需求描述： 页面同一时间只能出现一个错误提示
 */
// import { Notification, Message } from 'element-plus'
import { ElMessage, ElNotification } from 'element-plus'

let MyMessage = ElMessage
let MyNotification = ElNotification

var NOW_ACTIVE_TIP = false // 当前是否有提示框展示

/**
 * 是否有提示框展示
 * @returns {
 *  messageCount
 *  notificationCount
 * }
 */
function hasTip() {
  // let hasMessage  hasNotification
  const tipCount = {
    messageCount: document.querySelectorAll('.el-message').length, // 是否有 Message 类型的提示
    notificationCount: document.querySelectorAll('.el-notification').length // 是否有 Notification 类型的提示
  }
  NOW_ACTIVE_TIP = tipCount.messageCount !== 0 || tipCount.notificationCount !== 0
  console.log('👉👉👉进入判断是否有提示的逻辑 -- NOW_ACTIVE_TIP', NOW_ACTIVE_TIP, tipCount)
  return tipCount
}

// Message 类型消息 原始方法
const OldMessage = MyMessage
const OldMessageSuccess = MyMessage['success']
const OldMessageWarning = MyMessage['warning']
const OldMessageInfo = MyMessage['info']
const OldMessageError = MyMessage['error']
// message 默认提示
MyMessage = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldMessage(options)
}
MyMessage['success'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldMessageSuccess(options)
}
MyMessage['warning'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldMessageWarning(options)
}
MyMessage['info'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldMessageInfo(options)
}
MyMessage['error'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldMessageError(options)
}

// Notification 类型消息 原始方法
const oldNotification = MyNotification
const OldNotificationSuccess = MyNotification['success']
const OldNotificationWarning = MyNotification['warning']
const OldNotificationInfo = MyNotification['info']
const OldNotificationError = MyNotification['error']
// notification 默认提示
MyNotification = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return oldNotification(options)
}
MyNotification['success'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldNotificationSuccess(options)
}
MyNotification['warning'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldNotificationWarning(options)
}
MyNotification['info'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldNotificationInfo(options)
}
MyNotification['error'] = function(options) {
  hasTip() // 检查是否已有提示
  if (NOW_ACTIVE_TIP) return
  else return OldNotificationError(options)
}

console.log('👉👉👉加载统一提示逻辑 完成')

export default {
  Message: MyMessage,
  Notification: MyNotification
}
